package com.teamwork_backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teamwork_backend.entity.Experiences;
import com.teamwork_backend.service.ExperiencesService;
import com.teamwork_backend.service.ExperiencesService;
import com.teamwork_backend.utils.DataResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author windy
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/experiences")
@Api(value = "ExperiencesController", tags = {"简历的经历信息操作接口"})
public class ExperiencesController {
    @Autowired
    private ExperiencesService experiencesService;

    /** 获得某简历的经历信息接口 */
    @GetMapping("getexperiences") // Get方法
    @ApiOperation(value = "获得某简历的经历信息接口", notes = "获得某简历的经历信息接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resumeid", value = "简历唯一标示", required = true, dataType = "string")})
    // GET请求会直接在函数括号里面跟参数，并且写在外面写@ApiImplicitParams注解
    // POST请求需要写@RequestBody请求体，通过参数对象来传参数，一定要注意否则前端Vue+axios接收不了参数
    public DataResult getexperiences(String resumeid) {
        QueryWrapper<Experiences> wrapper = new QueryWrapper<>();
        // 并以order顺序权重从小到大
        wrapper.select().eq("resumeid",resumeid).orderByAsc("`order`");

        List<Experiences> experiencesList = experiencesService.list(wrapper);
        if(experiencesList != null) {
            // 成功返回数据
            return DataResult.finished().data("data", experiencesList).message("某简历的经历信息查找成功！");
        } else {
            // 失败返回数据
            return DataResult.failed().message("某简历的经历信息不存在！");
        }
    }

    /**增加某简历的经历信息接口*/
    @PostMapping("addexperiences") // Post方法
    @ApiOperation(value = "增加某简历的经历信息接口", notes = "增加某简历的经历信息接口", httpMethod = "POST")
    public DataResult addexperiences(
            @ApiParam(name = "experiences", value = "奖项库信息对象", required = true)
            @RequestBody Experiences experiences)
    {
        // 设置添加成功的返回标志
        boolean flag = experiencesService.save(experiences);
        if (flag) {
            // 成功返回数据
            return DataResult.finished().message("某简历的经历信息增加成功！");
        } else {
            // 失败返回数据
            return DataResult.failed().message("某简历的经历信息增加失败！");
        }
    }

    /**更新某简历的经历信息接口*/
    @PostMapping("updateexperiences") // Post方法
    @ApiOperation(value = "更新某简历的经历信息接口", notes = "更新某简历的经历信息接口", httpMethod = "POST")
    public DataResult updateexperiences(
            @ApiParam(name = "experiences", value = "个人/奖项库信息对象", required = true)
            @RequestBody Experiences experiences)
    {
        if(experiences.getResumeid().equals(""))
            experiences.setResumeid(null);
        // 设置添加成功的返回标志
        boolean flag = experiencesService.updateById(experiences);
        if (flag) {
            // 成功返回数据
            return DataResult.finished().message("某简历的经历信息更新成功！");
        } else {
            // 失败返回数据
            return DataResult.failed().message("某简历的经历信息更新失败！");
        }
    }

    /**删除某简历的经历信息接口*/
    @PostMapping("delexperiences") // Post方法
    @ApiOperation(value = "删除某简历的经历信息接口", notes = "删除某简历的经历信息接口", httpMethod = "POST")
    public DataResult delexperiences(
            @ApiParam(name = "experiences", value = "个人/奖项库信息对象", required = true)
            @RequestBody Experiences experiences)
    {
        // 设置添加成功的返回标志
        boolean flag = experiencesService.removeById(experiences);
        if (flag) {
            // 成功返回数据
            return DataResult.finished().message("某简历的经历信息删除成功！");
        } else {
            // 失败返回数据
            return DataResult.failed().message("某简历的经历信息删除失败！");
        }
    }
}

